package com.dhp.campus.utils;

import cn.hutool.core.date.DateTime;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssUtil {
    private String accessKeyID;
    private String accessKeySecret;
    private String endpoint;
    private String bucketName;


    public String uploadFile(MultipartFile file) throws Exception {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyID, accessKeySecret);

        try {
            InputStream inputStream = file.getInputStream();
            String fileName = UUID.randomUUID()+file.getOriginalFilename() ;
            /**
             * 使用的依赖是：
             * <dependency>
             *    <groupId>joda-time</groupId>
             *    <artifactId>joda-time</artifactId>
             *</dependency>
             * new DateTime().toString(),toString()传入日期规则
             * yyyy/MM-dd   形成 年为一个文件夹，月-日为一个文件夹
             */
            String date = new DateTime().toString("yyyy/MM-dd");
            //  2019/7-30/xdyguhn41601.jpg
            String filePath = date + "/" + fileName;
            /**
             * 第一个参数    bucket名字
             * 第二个参数    上传文件到阿里云OSS的文件路径和文件名称，filePath = path + fileName
             *              eg:/a/b/c.jpg
             * 第三个参数    是文件输入流
             */
            ossClient.putObject(bucketName, filePath, inputStream);
            // https://six-teen.oss-cn-beijing.aliyuncs.com/favicon.png
            // 即https:// + bucketName +. +endpoint + / +文件名
            String url = "https://" + bucketName + "." + endpoint + "/" + filePath;
            return url;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "";
    }
}
